import SendMsg from "@/api/SendMsg";
import {message} from "ant-design-vue";
import router from "@/router";
import i18n from "../../../i18n/index";

const js1000 = {
    namespaced: true,
    state: {
        // table数据
        dataSource: [],
        dataSource2: [],
        //VI table数据
        // table数据
        dataSourceVI: [],
        dataSourceVI2: [],
        dataSourceVIDetail: [],
        //弹窗
        // WATSummaryReportVisible:false,
        // CPSummaryReportVisible:false,
        VisualInspectionReportVisible:false,
        CustomerWaiveFormVisible:false,
        ShippingSheetVisible:false,
        VisualInspectionReportDetailVisible:false,
        printerVisible: false,
        // 查询条件
        queryForm: {
            crrId: '',
            lotId: '',
            printName: '',
            lotIdLike: '',
            carierIdLike: '',
            state: 'non-Hold'

        },
        queryLotInfoForm: {
            technology: '',
            productName: '',
            customerCode: '',
            quantity: '',
            evtUsr: '',
            evtTimestamp: '',
            lotId: '',
        },
        lotList: [],
        printerInformationList:[],
        loadTable: false,
        // 编辑表单数据
        capForm: {
            lotId: '',
            quantity: 0,
            crrId: undefined,
            priInt: undefined,
            stepStatus: undefined,
            productName: undefined,
            waitTime: 0,
            type: '',
            srcType: '',
            result: undefined,
        },
        ShippingSheet: {
            customerName: '',
            customerCode: '',
            lotId: '',
            quantity: '',
            waferSlots: '',
        },
        CustomerWaiveForm: {
            lcbh: '',
            productName: '',
            lotId: '',
            quantity: '',
            waferId: '',
            waiveItem: '',
            waiveComment: '',
            effectiveDate: '',
        },
        dataPagination: {
            current: 1,
            pageSize:15,
            total: 0,
            showTotal: (total, range) => {
                return i18n.t('menuItem.total')+` ${total} `+i18n.t('menuItem.term')
            },
        },
        // table选中行
        selectRow: {},
    },
    mutations: {
        //查询table
        queryLotInfo(state) {
            let codeInTrx = {
                trxId: "retLot",
                actionFlg: "Q",
                pageSize: state.dataPagination.pageSize,
                pageNum: state.dataPagination.current,
                lotId: state.queryForm.lotIdLike,
                carrierId: state.queryForm.carierIdLike,
                state: state.queryForm.state
            };
            state.dataSource = []
            state.dataSource2 = []
            state.loadTable = true
            SendMsg.sendPostTrx(codeInTrx).then((res) => {
                let outTrx = res.data;
                if ('0000000' === outTrx.rtnCode) {
                    state.dataSource = outTrx.oaryA.map((item, index) => {
                        item.key = (index + 1);
                        return item;
                    });
                }
                state.dataPagination.total = outTrx.tblCnt;
            }).finally(() => {
                state.loadTable = false
            });
        },
        //查询table2
        queryRetWaferInfo(state,val) {
            let codeInTrx = {
                trxId: "normalLot",
                actionFlg: "QWAFER",
                lotId: val
            };
            state.dataSource2 = []
            state.loadTable = true
            SendMsg.sendPostTrx(codeInTrx).then((res) => {
                let outTrx = res.data;
                if ('0000000' === outTrx.rtnCode) {
                    state.dataSource2 = outTrx.normalLotOBList.map((item, index) => {
                        item.key = (index + 1);
                        return item;
                    });
                }
            }).finally(() => {
                state.loadTable = false
            });
        },
        //table分页
        setDataPagination(state, val) {
            state.dataPagination = val;
        },
        // 清空弹窗数据
        cleanPackagingSummary(state) {
            state.queryForm = {
                crrId: '',
                lotId: '',
            }
            state.dataSource = []
            state.dataSource2 = []
            state.dataPagination = {
                size:"small",
                current: 1,
                pageSize: 10,
                total: 0,
                showTotal: (total, range) => {
                    return i18n.t('menuItem.total')+` ${total} `+i18n.t('menuItem.term')
                },
            }
        },
        clickCell(state,val){
            // if(val.type === 'CP' && val.srcType === 'CP'){
            //     state.CPSummaryReportVisible = true
            // }else{
            //     state.WATSummaryReportVisible = true
            // }
            if(!state.queryForm.carierIdLike){
                message.warn("Please read first carrier id")
                return
            }
            state.selectRow = {
                lotId: val.lotId,
                type: val.type,
                srcType: val.srcType,
            }
            state.queryForm.lotId = val.lotId
            state.VisualInspectionReportVisible = true
            js1000.mutations.querySelectList(state)
            js1000.mutations.queryLotComment(state)
            js1000.mutations.queryTableList(state)
            js1000.mutations.queryEngTableList(state)
        },
        //表格行数据
        customRow(state, obj) {
            // state.selectRow = {
            //     lotId: obj.lotId,
            //     type: obj.type,
            //     srcType: obj.srcType,
            // }
            //
            // state.queryForm.lotId = obj.lotId
        },
        VisualInspectionReportOk(state){
            state.VisualInspectionReportVisible = false
            js1000.mutations.queryCustomerWaiveForm(state)
        },
        VisualInspectionReportDetailOk(state){
            state.VisualInspectionReportDetailVisible = false
            state.VisualInspectionReportVisible = true
        },
        VisualInspectionReportCancel(state){
            state.VisualInspectionReportVisible = false
        },
        CustomerWaiveFormOk(state){
            state.CustomerWaiveFormVisible = false
            state.ShippingSheetVisible = true
            js1000.mutations.queryShippingSheetList(state)
        },
        CustomerWaiveFormCancel(state){
            state.CustomerWaiveFormVisible = false
        },
        ShippingSheetOk(state){
            state.ShippingSheetVisible = false
            state.printerVisible = true
            state.queryForm.printName = ''
            js1000.mutations.queryPrinterInformationList(state)

        },
        ShippingSheetCancel(state){
            state.ShippingSheetVisible = false
        },
        printerOk(state){
            if(!state.queryForm.printName){
                message.warn(i18n.t("Please select a printer"))
                return
            }
            state.printerVisible = false
            // js1000.mutations.queryRetWaferInfo(state)
            js1000.mutations.BtwPrint(state)
        },
        printerCancel(state){
            state.printerVisible = false
        },

        //Visual Inspection Report

        //查询table1
        queryTableList(state) {
            let codeInTrx = {
                trxId: "retLot",
                actionFlg: "QMA",
                lotId: state.selectRow.lotId,
            };
            state.dataSourceVI = []
            state.dataSourceVI2 = []
            state.loadTable = true
            SendMsg.sendPostTrx(codeInTrx).then((res) => {
                let outTrx = res.data;
                if ('0000000' === outTrx.rtnCode) {
                    state.dataSourceVI = outTrx.oaryD.map((item, index) => {
                        item.key = (index + 1);
                        return item;
                    });
                }
            }).finally(() => {
                state.loadTable = false
            });
        },
        //查询table2
        queryEngTableList(state) {
            let codeInTrx = {
                trxId: "retLot",
                actionFlg: "QENG",
                lotId: state.selectRow.lotId,
            };
            state.dataSourceVI2 = []
            state.loadTable = true
            SendMsg.sendPostTrx(codeInTrx).then((res) => {
                let outTrx = res.data;
                if ('0000000' === outTrx.rtnCode) {
                    state.dataSourceVI2 = outTrx.oaryE.map((item, index) => {
                        item.key = (index + 1);
                        return item;
                    });
                }
            }).finally(() => {
                state.loadTable = false
            });
        },
        //查询table MA detail info
        queryTableDetailList(state) {
            let codeInTrx = {
                trxId: "retLot",
                actionFlg: "QVIDETAIL",
                lotId: state.queryForm.lotId,
                waferId: state.selectRow.waferId,
                site: state.selectRow.site,
                deptType: "MA",
            };
            state.dataSourceVIDetail = []
            state.loadTable = true
            SendMsg.sendPostTrx(codeInTrx).then((res) => {
                let outTrx = res.data;
                if ('0000000' === outTrx.rtnCode) {
                    state.dataSourceVIDetail = outTrx.oaryF.map((item, index) => {
                        item.key = (index + 1);
                        return item;
                    });
                }
            }).finally(() => {
                state.loadTable = false
            });
        },
        //查询table Eng detail info
        queryEngTableDetailList(state) {
            let codeInTrx = {
                trxId: "retLot",
                actionFlg: "QVIDETAIL",
                lotId: state.queryForm.lotId,
                waferId: state.selectRow.waferId,
                site: state.selectRow.site,
                deptType: "ENG",
            };
            state.dataSourceVIDetail = []
            state.loadTable = true
            SendMsg.sendPostTrx(codeInTrx).then((res) => {
                let outTrx = res.data;
                if ('0000000' === outTrx.rtnCode) {
                    state.dataSourceVIDetail = outTrx.oaryF.map((item, index) => {
                        item.key = (index + 1);
                        return item;
                    });
                }
            }).finally(() => {
                state.loadTable = false
            });
        },
        // 查询lot comment数据
        queryLotComment(state) {
            let intrx = {
                trxId: "retLot",
                actionFlg: "QCOMMENT",
                lotId: state.selectRow.lotId,
            };
            SendMsg.sendPostTrx(intrx).then((res) => {
                let outTrx = res.data;
                if ('0000000' === outTrx.rtnCode) {
                    state.capForm.comment = outTrx.dnComment
                }
            })
        },
        // lot下拉框带出相关信息
        querySelectList(state) {
            state.queryLotInfoForm = {
                technology: '',
                productName: '',
                customerCode: '',
                quantity: '',
                evtUsr: '',
                evtTimestamp: '',
                lotId: '',
            }
            let intrx = {
                trxId: "retLot",
                actionFlg: "QSELECT",
                lotId: state.selectRow.lotId,
            };
            SendMsg.sendPostTrx(intrx).then((res) => {
                let outTrx = res.data;
                if ('0000000' === outTrx.rtnCode) {
                    if (outTrx.oaryC.length > 0) {
                        state.queryLotInfoForm = outTrx.oaryC[0];
                    }
                }
            })
        },
        // 清空弹窗数据
        cleanData(state) {
            state.queryForm = {
                crrId: '',
                lotId: '',
                printName: '',
            }
            state.queryLotInfoForm = {
                technology: '',
                productName: '',
                customerCode: '',
                quantity: '',
                evtUsr: '',
                evtTimestamp: '',
                lotId: '',
            }
            state.dataSourceVI = []
            state.dataSourceVI2 = []
        },

        clickCellViDetail(state,record){
            state.VisualInspectionReportDetailVisible = true
            state.capForm.evtCate = 'WJGE'
            state.selectRow = {
                lotId: record.lotId,
                waferId: record.waferId,
                site: record.site,
            }
            js1000.mutations.queryTableDetailList(state);
        },
        clickCellEng(state,record){
            state.VisualInspectionReportDetailVisible = true
            state.capForm.evtCate = 'EJGE'
            state.selectRow = {
                lotId: record.lotId,
                waferId: record.waferId,
                site: record.site,
            }
            js1000.mutations.queryEngTableDetailList(state);
        },

        //Customer Waive Form

        //查询Form
        queryCustomerWaiveForm(state) {
            state.CustomerWaiveForm = {}
            let codeInTrx = {
                trxId: "retLot",
                actionFlg: "QOAREPORT",
                lotId: state.queryForm.lotId,
            };
            SendMsg.sendPostTrx(codeInTrx).then((res) => {
                let outTrx = res.data;
                if ('0000000' === outTrx.rtnCode) {
                    if (outTrx.oaryAReport != null){
                        state.CustomerWaiveForm = outTrx.oaryAReport;
                    }
                    //如果 Customer Waive Form 没有数据，直接打开 Shipping Sheet
                    if (state.CustomerWaiveForm.lcbh === undefined && state.CustomerWaiveForm.productName === undefined
                        && state.CustomerWaiveForm.lotId === undefined && state.CustomerWaiveForm.quantity === undefined
                        && state.CustomerWaiveForm.waferId === undefined && state.CustomerWaiveForm.waiveItem === undefined
                        && state.CustomerWaiveForm.waiveComment === undefined && state.CustomerWaiveForm.effectiveDate === undefined){
                        state.ShippingSheetVisible = true
                        js1000.mutations.queryShippingSheetList(state)
                    }else{
                        state.CustomerWaiveFormVisible = true
                    }
                }
            })
        },

        //Shipping Sheet

        //查询table
        queryShippingSheetList(state) {
            state.ShippingSheet = {}
            let codeInTrx = {
                trxId: "retLot",
                actionFlg: "QSELECT",
                lotId: state.queryForm.lotId,
            };
            SendMsg.sendPostTrx(codeInTrx).then((res) => {
                let outTrx = res.data;
                if ('0000000' === outTrx.rtnCode) {
                    if (outTrx.oaryC.length > 0) {
                        state.ShippingSheet = outTrx.oaryC[0];
                        state.ShippingSheet.waferSlots = outTrx.waferSlots
                    }
                }
            })
        },

        //查询打印机信息
        queryPrinterInformationList(state){
            let codeInTrx = {
                trxId: "retLot",
                actionFlg: "QP",
            };
            SendMsg.sendPostTrx(codeInTrx).then((res) => {
                let outTrx = res.data;
                if ('0000000' === outTrx.rtnCode) {
                    state.printerInformationList = outTrx.oaryP
                }
            })
        },
        //打印
        BtwPrint(state){
            let codeInTrx = {
                trxId: "retLot",
                actionFlg: "BTW",
                lotId: state.queryForm.lotId,
                productName: state.queryLotInfoForm.productName,
                customerCode: state.queryLotInfoForm.customerCode,
                printName: state.queryForm.printName.substring(state.queryForm.printName.indexOf(":")+1,state.queryForm.printName.length),
                printIp: state.queryForm.printName.substring(0,state.queryForm.printName.indexOf(":")),
                printCnt: 3,
            };
            SendMsg.sendPostTrx(codeInTrx).then((res) => {
                let outTrx = res.data;
                if (outTrx.rePrint === false) {
                    message.warn("Reprint requires going to the Reprint page")
                    return
                }
                if ('0000000' === outTrx.rtnCode) {
                    message.success("Printing successful！")
                }
            })
        },
    },
    getters: {},
    action: {}
}
export default js1000;
